package org.celery.modules.order.wrapper;

import org.celery.common.utils.IPUtil;
import org.celery.modules.goods.entity.Goods;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.service.IGoodsService;
import org.celery.modules.order.enums.*;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.celery.modules.order.entity.Order;
import org.celery.modules.order.vo.OrderVO;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;

import java.util.List;

/**
 * 订单表包装类,返回视图层所需的字段
 *
 * @author Celery
 * @since 2022-04-04
 */
public class OrderWrapper extends BaseEntityWrapper<Order, OrderVO>  {

    public static OrderWrapper build() {
        return new OrderWrapper();
    }

	@Override
	public OrderVO entityVO(Order order) {
		OrderVO orderVO = BeanUtil.copy(order, OrderVO.class);
		orderVO.setPayStatusName(OrderStatusPayEnum.getName(order.getPayStatus()));
		orderVO.setShipStatusName(OrderStatusShipEnum.getName(order.getShipStatus()));
		orderVO.setStatusName(OrderStatusEnum.getName(order.getStatus()));
		orderVO.setPayChannelName(PayChannelEnum.getName(order.getPayChannel()));
		orderVO.setShipTypeName(ShipTypeEnum.getName(order.getShipType()));

		if (!order.getFromIp().contains("-")) {
			String city = IPUtil.getCityInfo(order.getFromIp());
			if (Func.isNotEmpty(city)) {
				orderVO.setFromIp(city + "-" + order.getFromIp());
			}
		}

		// 默认设置关联商品第一项的商品名
		List<GoodsStyle> associatedGoodsList = JsonUtil.parseArray(order.getAssociatedGoods(), GoodsStyle.class);
		if (Func.isNotEmpty(associatedGoodsList)) {
			IGoodsService goodsService = SpringUtil.getBean(IGoodsService.class);
			Goods goods = goodsService.getById(associatedGoodsList.get(0).getGoodsId());
			if (Func.isNotEmpty(goods)) {
				orderVO.setGoodsName(goods.getName());
			}
		}

		return orderVO;
	}

}
